package engine.scene.player
{
	import engine.data.ModelLocator;
	import engine.display.newAttack.action.effect.PointPlayLite;
	import engine.event.RoleEffectEvent;
	import engine.logic.McPool;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import newx.StageInfo;
	import newx.com.ResManager;

	/**
	 *	主角特效 
	 * @author lw
	 * 
	 */
	public class RoleModelEffect
	{
		private var _model		:	ModelLocator;
		
		private var _place		:	Sprite;
		
		private var _roleModel	:	RoleModel;
		
		private var _firstCompleteTask : Boolean = false;
	
		private var _effectPoint:Point	=new Point();
		public function RoleModelEffect( place : Sprite, roleModel : RoleModel )
		{
			_roleModel = roleModel;
			
			_place 	=	place;
			
			_model	=	ModelLocator.getInstance();
			
			_model.addEventListener( RoleEffectEvent.SHOW_EFFECT, onShowEffect );
		}
		
		private function onShowEffect( evt : RoleEffectEvent ):void
		{
			arrSprite(_roleModel,new Array());
			var point:Point	=	new Point(StageInfo.stage.stageHeight/2+100,StageInfo.stage.stageHeight/2-200)
			switch( evt.effectType )
			{
				case RoleEffectEvent.LEVEL_UP:
				{
//					setTimeout( function ():void
//					{
//						
//						//showEffect( McPool.borrowRes( "upgrade" ), _effectPoint );
//					},200)
					//特殊处理 三级 升 四级
					//if(_model.scene.roleLevel == 3)return
					showEffect( McPool.borrowRes( "upgrade" ), _effectPoint );
					break;
				}
				case RoleEffectEvent.ACCEPT_TASK:
				{
					showEffect( McPool.borrowRes( "MC_receiveOK" ),point );
					break;
				}
				case RoleEffectEvent.COMPLETE_TASK:
				{
//					if( !_firstCompleteTask )
//					{
//						ResManager.loadRes( "MC_receiveOK", "res/swf/MC_QuestOK.swf" );
//						_firstCompleteTask = true;
//					}
//					showEffect( ResManager.newResClass( "MC_QuestOK" ) as MovieClip,point );
					showEffect(McPool.borrowRes("MC_QuestOK") as MovieClip,point);
					break;
				}
			}
		}
		
		private function showEffect( swf : MovieClip, startPoint : Point ):void
		{
			//mc是特效所在的层
			var mc:Sprite	=	StageInfo.guideSpace as Sprite;	
			mc.mouseChildren	=	mc.mouseEnabled	=	false;
			new PointPlayLite( mc, startPoint, swf );
		}
		
//		private function showUpEffect( swf : MovieClip, startPoint : Point ):void
//		{
//			new PointPlayLite( _place, startPoint, swf );
//		}
		
		/**
		 * 特效坐标转化 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function countEffectPoint( arr:Array ):Point
		{
			var point:Point	=	new Point();	
			point.x			=	_roleModel.x;
			point.y			=	_roleModel.y-50;
			for( var i:int= 0; i < arr.length-1;i++ )
			{
				point.x = point.x+( arr[i] as DisplayObject ).x;
				point.y = point.y+( arr[i] as DisplayObject ).y;
			}
			return point
		}
		
		/**
		 * 得到_roleModel到mainSpace每一个父容器
		 * @param mc
		 * @param arr
		 * 
		 */		
		private function arrSprite( mc:DisplayObject,arr:Array ):void
		{
			if( mc.parent == StageInfo.stage || mc.parent == null)
			{
				_effectPoint	=	countEffectPoint( arr );
				return
			}
			arr.push( mc.parent );
			arrSprite( mc.parent,arr );
		}
	}
}